﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DotNetX
{
    public static class ObjectExtensions
    {
        public static bool IsInt32(this object o)
        {
            if (o == null)
                return false;

            if (ObjectExtensions.IsString(o))
            {
                int r;
                return int.TryParse(o.ToString(), out r);
            }
            else
            {
                return o is Int32;
            }
        }

        public static bool IsFloat(this object o)
        {
            if (o == null)
                return false;

            if (ObjectExtensions.IsString(o))
            {
                float r;
                return float.TryParse(o.ToString(), out r);
            }
            else
            {
                return o is float;
            }
        }

        public static bool IsString(this object o)
        {
            if (o == null)
                return false;

            return o is String;
        }

        public static bool IsBoolean(this object o)
        {
            if (o == null)
                return false;

            if (ObjectExtensions.IsString(o))
            {
                bool r;
                return bool.TryParse(o.ToString(), out r);
            }
            else
            {
                return o is Boolean;
            }
        }

        public static bool IsDateTime(this object o)
        {
            if (o == null)
                return false;

            if (ObjectExtensions.IsString(o))
            {
                DateTime d;
                return DateTime.TryParse(o.ToString(), out d);
            }
            else
            {
                return o is DateTime;
            }
        }

        public static int ToInt32OrZero(this object o)
        {
            return ObjectExtensions.ToInt32OrDefault(o, 0);
        }

        public static int ToInt32OrDefault(this object o, int def)
        {
            if (o == null)
                return def;

            if (o.IsInt32())
                return Convert.ToInt32(o);
            else
                return def;
        }

        public static float ToFloatOrZero(this object o)
        {
            return ObjectExtensions.ToFloatOrDefault(o, 0.0f);
        }

        public static float ToFloatOrDefault(this object o, float def)
        {
            if (o == null)
                return def;

            if (o.IsFloat())
                return (float)Convert.ToDouble(o);
            else
                return def;
        }

        public static bool ToBooleanOrDefault(this object o, bool def)
        {
            if (o == null)
                return def;

            if (o.IsBoolean())
                return Convert.ToBoolean(o);
            else
                return def;
        }

        public static DateTime ToDateTimeOrMinValue(this object o)
        {
            if (o == null)
                return DateTime.MinValue;

            if (o.IsDateTime())
                return Convert.ToDateTime(o);
            else
                return DateTime.MinValue;
        }

        public static string ToEmptyStringIfNull(this object o)
        {
            if (o == null)
                return String.Empty;
            else
                return o.ToString();
        }
    }
}
